package com.aircjm.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Scanner;

import org.junit.Test;

/**
 * Created by chenjiaming on 2016/7/28. socket编程也就是网络编程，通过两个socket进行IO通信，进行数据的传输
 */
public class SocketTest {

    /**
     * 简单发送UDP协议消息
     * 
     * @throws IOException
     */
    @Test
    public void sendUdpMessage() throws IOException {

        // 创建socket对象
        DatagramSocket datagramSocket = new DatagramSocket();

        // 具体业务数据
        byte[] buf = "Hello World UDP".getBytes();

        // 数据打包成一个数据包裹
        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10008);

        // 发送数据包裹
        datagramSocket.send(datagramPacket);

        // 释放资源
        datagramSocket.close();
    }

    /**
     * 简单接受UDP协议消息
     * 
     * @throws IOException
     */
    @Test
    public void receiveUdpMessage() throws IOException {

        // socket连接对象
        DatagramSocket datagramSocket = new DatagramSocket(10008);

        // 创建一个空的数据包裹
        byte[] buf = new byte[1000];

        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);

        // 接受数据包裹
        datagramSocket.receive(datagramPacket);

        // 获取数据包裹里面数据
        String s = new String(datagramPacket.getData());
        System.out.println(s);
        System.out.println("IP 地址为： " + datagramPacket.getAddress().getHostAddress() + "， 主机名称为： " + datagramPacket.getAddress().getHostName());

        // 释放资源
        datagramSocket.close();
    }

    @Test
    public void testUdpSend() throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();

        byte[] buf = "hello".getBytes();
        int length = buf.length;
        InetAddress address = InetAddress.getByName("172.31.2.59");
        int port = 10086;
        DatagramPacket p = new DatagramPacket(buf, length, address, port);
        datagramSocket.send(p);
        datagramSocket.close();

    }

    @Test
    public void testUdpReceive() throws IOException {

        DatagramSocket datagramSocket = new DatagramSocket(10086);
        byte[] data = new byte[1024];
        DatagramPacket dp = new DatagramPacket(data, data.length);
        datagramSocket.receive(dp);
        byte[] data1 = dp.getData();
        String s = new String(data1, 0, data1.length);
        System.out.println(s);
        datagramSocket.close();
    }

    @Test
    public void testSocket() throws IOException {
        Socket s = new Socket("128.0.0.1", 443);

        s.setSoTimeout(10000);

        InputStream inputStream = s.getInputStream();
        Scanner in = new Scanner(inputStream);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            System.out.println(line);
        }
    }

    @Test
    public void testSocket1() throws IOException {
        Socket s = new Socket();

        s.connect(new InetSocketAddress("127.0.0.1", 8080), 10000);

        s.setSoTimeout(10000);

        InputStream inputStream = s.getInputStream();
        Scanner in = new Scanner(inputStream);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            System.out.println(line);
        }
    }

    @Test
    public void testSocketInetAddress() throws IOException {
        InetAddress inetAddress = InetAddress.getByName("google.com");
        System.out.println(inetAddress.getHostName());
        System.out.println(Arrays.toString(inetAddress.getAddress()));

        InetAddress[] allByName = InetAddress.getAllByName("baidu.com");
        for (InetAddress address : allByName) {
            System.out.println(address.getHostAddress());
        }
    }

    @Test
    public void getLocalHostTest() throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();
        System.out.println(inetAddress.getHostName());
        System.out.println(inetAddress.getHostAddress());
        System.out.println(Arrays.toString(inetAddress.getAddress()));
    }

    @Test
    public void testServerSocket() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8189);

        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();

        OutputStream outputStream = socket.getOutputStream();

        Scanner scanner = new Scanner(inputStream);

        PrintWriter printWriter = new PrintWriter(outputStream, true);

        printWriter.print("hello ! enter bye to exit.");

        boolean done = false;

        while (!done && scanner.hasNextLine()) {
            String line = scanner.nextLine();
            printWriter.print("Echo: " + line);
            if (line.trim().equals("bye")) {
                done = true;
            }
        }

    }

}
